
*  Merge Marriage with Separation data 

*  Marriages 

clear 
set more off
  
use "$data\M_final_dataset.dta" , clear
rename y_survey ym 
tab ym
keep if ym>=1998
count
* Drop duplicates id
duplicates drop ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov , force
count 
save "$data\M_final_merge.dta" , replace

 
*  Separations 
 
use "$data\S_final_data.dta" , clear

* Select sample of marriages after 1998
destring ym day_marriage m_marriage h_born_d h_born_m w_born_d w_born_m h_born_y w_born_y , replace
keep if ym>=1998 
tab ym 
tab y_survey

* Select sample of residents in Italy (focus on Italian marriage market)
codebook h_resid_prov
recode h_resid_prov(.=888)
codebook w_resid_prov
recode w_resid_prov(.=888)
tab h_resid_prov y_survey if h_resid_prov==888
tab w_resid_prov y_survey if w_resid_prov==888
count if w_resid_prov==888 & h_resid_prov==888 
drop if w_resid_prov==888 & h_resid_prov==888  

* Drop duplicates id
duplicates tag ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov , gen (replication)
tab replication 
duplicates drop ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov , force

gen separated =1
keep replication sepdate y_survey separated ym m_marriage day_marriage  ///
     h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov h_born_region w_born_region

save "$data\S_final_merge.dta" , replace

  
* Merge Marriages with Separations 

* 1. merge: main variables
use "$data\M_final_merge.dta" , clear
    
recast float ym
recast float h_born_y
recast float w_born_y
merge 1:1 ym m_marriage day_marriage  h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov using "$data\S_final_merge.dta"
tab _merge

  preserve
  keep if _merge==3
  drop _merge
  save "$data\MS_merged1.dta" , replace
  restore
  
  preserve
  keep if _merge==2
  drop _merge
  
save "$data\S_compare.dta" , replace
  
 replace h_born_prov=90 if h_born_prov==104
 replace h_born_prov=91 if h_born_prov==105
 replace h_born_prov=92 if h_born_prov==106
 replace h_born_prov=92 if h_born_prov==107
 replace h_born_prov=15 if h_born_prov==108
 replace h_born_prov=44 if h_born_prov==109
 replace h_born_prov=72 if h_born_prov==110
 replace w_born_prov=90 if w_born_prov==104
 replace w_born_prov=91 if w_born_prov==105
 replace w_born_prov=92 if w_born_prov==106
 replace w_born_prov=92 if w_born_prov==107
 replace w_born_prov=15 if w_born_prov==108
 replace w_born_prov=44 if w_born_prov==109
 replace w_born_prov=72 if w_born_prov==110

keep sepdate y_survey separated ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov h_born_region w_born_region
duplicates drop ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov , force
save "$data\S_momerge1.dta" , replace
restore 
  
  preserve
  keep if _merge==1
  drop _merge
  
 replace h_born_prov=90 if h_born_prov==104
 replace h_born_prov=91 if h_born_prov==105
 replace h_born_prov=92 if h_born_prov==106
 replace h_born_prov=92 if h_born_prov==107
 replace h_born_prov=15 if h_born_prov==108
 replace h_born_prov=44 if h_born_prov==109
 replace h_born_prov=72 if h_born_prov==110
 replace w_born_prov=90 if w_born_prov==104
 replace w_born_prov=91 if w_born_prov==105
 replace w_born_prov=92 if w_born_prov==106
 replace w_born_prov=92 if w_born_prov==107
 replace w_born_prov=15 if w_born_prov==108
 replace w_born_prov=44 if w_born_prov==109
 replace w_born_prov=72 if w_born_prov==110
 
 drop sepdate y_survey separated replication
 duplicates drop ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov , force
 save "$data\M_nomerge1.dta" , replace
 restore 
    
  
* 2. merge: new province code
  
 clear
 use "$data\M_nomerge1.dta" , clear
 merge 1:1 ym m_marriage day_marriage  h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov using "$data\S_momerge1.dta"
 tab _merge
  
  preserve
  keep if _merge==3
  drop _merge
  save "$data\MS_merged2.dta" , replace
  restore
  
  preserve
  keep if _merge==2
  drop _merge
  keep sepdate y_survey separated ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov h_born_region w_born_region
	 
 * Change h_born_day
  duplicates drop ym m_marriage day_marriage h_born_y h_born_m h_born_prov w_born_y w_born_m w_born_day w_born_prov , force
  save "$data\S_momerge2.dta" , replace
  restore 
    
  preserve
  keep if _merge==1
  drop _merge
  drop sepdate y_survey separated 

 * Change h_born_day
  duplicates drop ym m_marriage day_marriage h_born_y h_born_m h_born_prov w_born_y w_born_m w_born_day w_born_prov , force
  save "$data\M_nomerge2.dta" , replace
  restore 

  
*  3. merge: change h_born_day  
 clear
 use "$data\M_nomerge2.dta" , clear
 merge 1:1 ym m_marriage day_marriage  h_born_y h_born_m h_born_prov w_born_y w_born_m w_born_day w_born_prov using "$data\S_momerge2.dta"
 tab _merge
 
  preserve
  keep if _merge==3
  drop _merge
  save "$data\MS_merged3.dta" , replace
  restore
  
  preserve
  keep if _merge==2
  drop _merge
  keep sepdate y_survey separated ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov h_born_region w_born_region
	 
 * Change w_born_day
  duplicates drop ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_prov , force
  save "$data\S_momerge3.dta" , replace
  restore 
    
  preserve
  keep if _merge==1
  drop _merge
  drop sepdate y_survey separated 

 * Change w_born_day
  duplicates drop ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_prov , force
  save "$data\M_nomerge3.dta" , replace
  restore 


* 4. merge: w_born_day  
 clear
 use "$data\M_nomerge3.dta" , clear
 merge 1:1 ym m_marriage day_marriage  h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m  w_born_prov using "$data\S_momerge3.dta"
 tab _merge
  
  preserve
  keep if _merge==3
  drop _merge
  save "$data\MS_merged4.dta" , replace
  restore
  
  preserve
  keep if _merge==2
  drop _merge
  keep sepdate y_survey separated ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov h_born_region w_born_region
	 
 * Change h_born_m 
  duplicates drop ym m_marriage day_marriage h_born_y h_born_day h_born_prov w_born_y w_born_m w_born_day  w_born_prov , force
  save "$data\S_momerge4.dta" , replace
  restore 
    
  preserve
  keep if _merge==1
  drop _merge
  drop sepdate y_survey separated 

 * Change h_born_m 
  duplicates drop ym m_marriage day_marriage h_born_y h_born_day h_born_prov w_born_y w_born_m w_born_day  w_born_prov , force
  save "$data\M_nomerge4.dta" , replace
  restore 

  
 * 5. merge: h_born_m    
 clear
 use "$data\M_nomerge4.dta" , clear
 merge 1:1 ym m_marriage day_marriage h_born_y h_born_day h_born_prov w_born_y w_born_m w_born_day  w_born_prov using "$data\S_momerge4.dta"
 tab _merge
  
  preserve
  keep if _merge==3
  drop _merge
  save "$data\MS_merged5.dta" , replace
  restore
  
  preserve
  keep if _merge==2
  drop _merge
  keep sepdate y_survey separated ym m_marriage day_marriage h_born_y h_born_m h_born_day h_born_prov w_born_y w_born_m w_born_day w_born_prov h_born_region w_born_region
	 
* Change w_born_m 
  duplicates drop ym m_marriage day_marriage h_born_y h_born_day h_born_m h_born_prov w_born_y w_born_day w_born_prov , force
  save "$data\S_momerge5.dta" , replace
  restore 
    
  preserve
  keep if _merge==1
  drop _merge
  drop sepdate y_survey separated 

* Change w_born_m 
  duplicates drop ym m_marriage day_marriage h_born_y h_born_day h_born_m h_born_prov w_born_y w_born_day  w_born_prov , force
 save "$data\M_nomerge5.dta" , replace
  restore 


* 6. merge : w_born_m 
 clear
 use "$data\M_nomerge5.dta" , clear
 merge 1:1 ym m_marriage day_marriage h_born_y h_born_day h_born_m h_born_prov w_born_y w_born_day  w_born_prov  using "$data\S_momerge5.dta"
 tab _merge
  
  preserve
  keep if _merge==2
  drop _merge
  save "$data\S_momerge_final.dta" , replace
  restore 

  drop if _merge==2
  drop _merge
  count
  
  append using "$data\MS_merged1.dta"
  append using "$data\MS_merged2.dta"
  append using "$data\MS_merged3.dta"
  append using "$data\MS_merged4.dta"
  append using "$data\MS_merged5.dta"
    
  tab separated
  recode separated (.=0)
  tab separated
  tabstat separated , by(marriage) stat(mean N sd)

* Correction: marriage date

tostring day_marriage m_marriage ym , replace

replace day_marriage="30" if day_marriage=="31" & m_marriage=="4"
replace day_marriage="30" if day_marriage=="31" & m_marriage=="6"
replace day_marriage="30" if day_marriage=="31" & m_marriage=="9"
replace day_marriage="30" if day_marriage=="31" & m_marriage=="11"
replace day_marriage="28" if day_marriage=="29" & m_marriage=="2"

foreach var in day_marriage m_marriage    {
replace `var'="01" if `var'=="1"
replace `var'="02" if `var'=="2"
replace `var'="03" if `var'=="3"
replace `var'="04" if `var'=="4"
replace `var'="05" if `var'=="5"
replace `var'="06" if `var'=="6"
replace `var'="07" if `var'=="7"
replace `var'="08" if `var'=="8"
replace `var'="09" if `var'=="9"
}
gen marriagedate= day_marriage+m_marriage+ym
replace sepdate = "31122012" if separated==0

* Marriage duration

gen mmdate = mofd(date(marriagedate, "DMY"))
gen msdate = mofd(date(sepdate, "DMY"))
format mmdate msdate %tm
gen md = (msdate-mmdate)
gen lmd = ln(msdate-mmdate)
drop if mmdate==.

* Merge with Singles 

destring ym, replace
merge m:1 provres ym hbc wbc using "$data\merge_singles_for_separations.dta" , keep(matched)
tab _merge 
drop _merge

drop ALLMsingle ALLFsingle ALLMsingle_for_wbc ALLFsingle_for_hbc 
tostring ym, replace

save "$data\MS_final_dataset.dta" , replace 



   
